package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.OmcBuildingSortMapper;
import com.csun.cmny.provider.model.domain.OmcBuildingSort;
import com.csun.cmny.provider.model.dto.BuildingSortSaveDto;
import com.csun.cmny.provider.model.vo.BuildingSortVo;
import com.csun.cmny.provider.service.OmcBuildingSortService;
import com.csun.cmny.util.PublicUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-03-11 11:18
 * @Description: the class omc building sort service impl
 */
@Slf4j
@Service
public class OmcBuildingSortServiceImpl extends BaseService<OmcBuildingSort>
        implements OmcBuildingSortService {

    @Resource
    private OmcBuildingSortMapper omcBuildingSortMapper;

    /**
     * query building sort
     *
     * @param groupId group id
     * @return the building sort vo
     */
    @Override
    public BuildingSortVo queryBuildingSort(Long groupId) {

        BuildingSortVo buildingSortVo = new BuildingSortVo();

        OmcBuildingSort omcBuildingSort = omcBuildingSortMapper.selectByPrimaryKey(groupId);
        if (PublicUtil.isEmpty(omcBuildingSort)) {
            return null;
        } else {
            BeanUtils.copyProperties(omcBuildingSort, buildingSortVo);
        }

        return buildingSortVo;
    }

    /**
     * save building sort
     *
     * @param buildingSortSaveDto save building sort dto
     * @param loginAuthDto        login auth dto
     * @return the int.
     */
    @Override
    public Integer saveBuildingSort(BuildingSortSaveDto buildingSortSaveDto, LoginAuthDto loginAuthDto) {

        Integer result = 0;

        OmcBuildingSort omcBuildingSort = omcBuildingSortMapper.selectByPrimaryKey(buildingSortSaveDto.getGroupId());
        if (PublicUtil.isEmpty(omcBuildingSort)) {
            omcBuildingSort = new OmcBuildingSort();
            omcBuildingSort.setUpdateInfo(loginAuthDto);
            BeanUtils.copyProperties(buildingSortSaveDto, omcBuildingSort);
            result = omcBuildingSortMapper.insertSelective(omcBuildingSort);
        } else {
            omcBuildingSort.setUpdateInfo(loginAuthDto);
            omcBuildingSort.setBuildingSort(buildingSortSaveDto.getBuildingSort());
            result = omcBuildingSortMapper.updateByPrimaryKeySelective(omcBuildingSort);
        }

        return result;
    }
}
